package monster.com.cvh.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import butterknife.BindView;
import butterknife.OnClick;
import monster.com.cvh.R;
import monster.com.cvh.activity.DownAndSendResumeActivity;
import monster.com.cvh.activity.LoginActivity;
import monster.com.cvh.constant.Constans;
import monster.com.cvh.event.RefreshResumeTempleIdEvent;
import monster.com.cvh.fragment.base.BaseFragment;
import monster.com.cvh.listener.EmailDialogListener;
import monster.com.cvh.util.CheckNetUtils;
import monster.com.cvh.util.SPUtils;
import monster.com.cvh.view.sement_control.SegmentControl;

/**
 * @author: 13580
 * @data: 2018/3/14
 * @function:简历Tab 简历预览Fragment
 */


public class ResumeFragment extends BaseFragment implements Observer, EmailDialogListener {
    private static final String TAG = "ResumeFragment";
    private static final String APP_ID = "wxe43215ee6c36efa2";
    @BindView(R.id.segment_control)
    SegmentControl mSegmentControl;
    @BindView(R.id.iv_resume_fragment_download)
    ImageView mIvDownload;
    @BindView(R.id.viewpager_resume_fragment)
    ViewPager mViewPager;
    private FragmentPagerAdapter mAdapter;
    private IWXAPI mIWXApi;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private int mTemplateId = 1;
    private boolean mHasLock = false;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        RefreshResumeTempleIdEvent.getInstance().addObserver(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RefreshResumeTempleIdEvent.getInstance().deleteObserver(this);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //初始化微信API
        mIWXApi = WXAPIFactory.createWXAPI(getContext(), APP_ID, true);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_resume;
    }

    @Override
    protected void initData() {
        mFragments.clear();
        mFragments.add(new UserMessageFragment());
        mFragments.add(new UserResumeFragment());
        mAdapter = new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }

            @Override
            public int getCount() {
                return mFragments.size();
            }
        };
        mViewPager.setAdapter(mAdapter);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 1) {
                    UserResumeFragment fragment = (UserResumeFragment) mFragments.get(position);
                    if (!CheckNetUtils.checkState_21()){
                        InfoUnCompeledDialogFragment.getInstance(getString(R.string.net_offline)).show(getFragmentManager(),"net_offline");
                    }
                    mIvDownload.setVisibility(View.VISIBLE);
                    fragment.refreshWebView();
                    fragment.setDialog();
                } else if (position == 0) {
                    mIvDownload.setVisibility(View.GONE);
                }
                mSegmentControl.setSelectedIndex(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void initEvent() {
        //选中监听
        mSegmentControl.setOnSegmentControlClickListener(new SegmentControl.OnSegmentControlClickListener() {
            @Override
            public void onSegmentControlClick(int index) {
                mViewPager.setCurrentItem(index, true);
            }
        });
    }

    @OnClick({R.id.iv_resume_fragment_download})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_resume_fragment_download:
                if (!isLogined()) {
                    Intent intent = new Intent(getContext(), LoginActivity.class);
                    intent.putExtra(Constans.VISITOR, Constans.LOGIN_WITHOUT_OPTION);
                    startActivityForResult(intent, Constans.LOGIN_WITHOUT_OPTION);
                    return;
                }
                DownResumeDialogFragment downResumeDialogFragment = new DownResumeDialogFragment().newInstance(mTemplateId);
                downResumeDialogFragment.setOnShowEmailListener(this);
                if (SPUtils.getBoolean(getContext(), Constans.SP_UNLOCK_RESUME, false)) {
                    downResumeDialogFragment.show(getFragmentManager(), "DownResumeDialogFragment");
                    return;
                }
                if (mHasLock) {
                    Intent intent = new Intent(getContext(), DownAndSendResumeActivity.class);
                    intent.putExtra(Constans.DOWN_RESUME, Constans.UNLOCK_RESUME);
                    startActivity(intent);
                } else {
                    downResumeDialogFragment.show(getFragmentManager(), "DownResumeDialogFragment");
                }
                break;
        }
    }

    @Override
    public void update(Observable observable, Object obj) {
        if (observable instanceof RefreshResumeTempleIdEvent) {
            if (obj instanceof Integer) {
                mTemplateId = (int) obj;
                UserResumeFragment fragment = (UserResumeFragment) mFragments.get(mFragments.size()-1);
                fragment.setResumeId(mTemplateId);
            } else if (obj instanceof Boolean) {
                mHasLock = (boolean) obj;
            }
        }
    }

    @Override
    public void onShowEmailDialog() {
        SendResumeDialogFragment fragment = SendResumeDialogFragment.getInstance(mTemplateId);
        fragment.show(getFragmentManager(), "send_resume");
    }
}
